import mongoose from "mongoose";
import { commLog, errorLog } from "./log";
import env from "@/utils/env";

const connectDB = async () => {
  const uri = `mongodb://${env.DB_HOST}:${env.DB_PORT}/${env.DB_NAME}`;

  try {
    await mongoose.connect(uri, { directConnection: true, replicaSet: "rs0" });
    commLog.info("数据库连接成功");
  } catch (error) {
    errorLog.error("数据库连接失败", error);
    process.exit();
  }
};

export default connectDB;
